using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectCamNang()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_CAMNANG
			};
			return View(CreateViewModel(data));
		}

		public ActionResult SelectCamNangPaging(int page = 1)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_CAMNANG,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListCamNang", CreateViewModel(data));
		}

		public ActionResult SelectCamNangByLoaiCamNang(int idloaicamnang, int page = 1)
		{
			var data = new DataTransferViewModel
			{
				IDLoaiCamNang = idloaicamnang,
				EnumViewModelType = EnumViewModel.ADMIN_CAMNANG,
				CurrentPage = page
			};
			return View("SelectCamNang", CreateViewModel(data));
		}

		public ActionResult SelectCamNangByNguoiDang(Guid idnguoidang, int page = 1)
		{
			var data = new DataTransferViewModel
			{
				IDNguoiDang = idnguoidang,
				EnumViewModelType = EnumViewModel.ADMIN_CAMNANG,
				CurrentPage = page
			};
			return View("SelectCamNang", CreateViewModel(data));
		}


		[AcceptVerbs(HttpVerbs.Delete)]
		public JsonResult DeleteCamNang(int id)
		{
			var deleteItem = _repCamNang.SelectByID(id);
			var fileName = deleteItem.HinhMinhHoa;
			var idloaicamnang = deleteItem.IDLoaiCamNang;
			var idnguoidang = deleteItem.IDNguoiDang;
			var result = _repCamNang.Delete(id);
			if (result)
			{
				if (fileName != "noimages.jpg")
				{
					var fullPath = Server.MapPath("/Content/Images/Items/" + fileName);
					DeleteFile(fullPath);
				}
			}
			return Json(new{
				Success = result,
				RecordCount = _repCamNang.GetTotalItemByLoaiCamNang(idloaicamnang),
				DeleteId = id
			});
		}

		public ActionResult DetailOfCamNang(int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_CAMNANG,
				EditID = id
			};
			return View(CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateCamNang(FormCollection forms)
		{
			string id = forms.GetValues("CamNang_ID")[0];
			string idloaicamnang = forms.GetValues("CamNang_IDLoaiCamNang")[0];
			string idnguoidang = forms.GetValues("CamNang_IDNguoiDang")[0];
			string tieude = forms.GetValues("CamNang_TieuDe")[0];
			string noidung = forms.GetValues("CamNang_NoiDung")[0];
			string noidungrutgon = forms.GetValues("CamNang_NoiDungRutGon")[0];
			var file = Request.Files["CamNang_HinhMinhHoa"];
			string refName = "";
			var editItem = _repCamNang.SelectByID(int.Parse(id));
			editItem.IDLoaiCamNang = int.Parse(idloaicamnang);
			editItem.IDNguoiDang = Guid.Parse(idnguoidang);
			editItem.TieuDe = tieude;
			editItem.NoiDung = noidung;
			editItem.NoiDungRutGon = noidungrutgon;
			var result = false;
			if (file.ContentLength != 0)
			{
				result = UploadFile(file, "/Content/Images/Items/", id, ref refName);
				editItem.HinhMinhHoa = refName;
			}
			result = _repCamNang.Save();
			var data = new DataTransferViewModel
			{
                IDLoaiCamNang = int.Parse(idloaicamnang),
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_CAMNANG,
				EditID = int.Parse(id),
				Updated = result,
			};
			return View("DetailOfCamNang", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertCamNang(FormCollection forms)
		{
			string idloaicamnang = forms.GetValues("CamNang_IDLoaiCamNang")[0];
			string idnguoidang = forms.GetValues("CamNang_IDNguoiDang")[0];
			string tieude = forms.GetValues("CamNang_TieuDe")[0];
			string noidung = forms.GetValues("CamNang_NoiDung")[0];
			string noidungrutgon = forms.GetValues("CamNang_NoiDungRutGon")[0];
			string ngaydang = forms.GetValues("CamNang_NgayDang")[0];
			var file = Request.Files["CamNang_HinhMinhHoa"];
			string refName = "";
			var newItem = new CamNang
			{
				IDLoaiCamNang = int.Parse(idloaicamnang),
				IDNguoiDang = Guid.Parse(idnguoidang),
				TieuDe = tieude,
				NoiDung = noidung,
				NoiDungRutGon = noidungrutgon,
				NgayDang = DateTime.Parse(ngaydang),
				HinhMinhHoa = "noimages.jpg"
			};
			var result = _repCamNang.Insert(newItem);
			if (result)
			{
				if (file.ContentLength != 0)
				{
					result = UploadFile(file, "/Content/Images/Items/", newItem.ID.ToString(), ref refName);
					if (result)
					{
						var addedItem = _repCamNang.SelectByID(newItem.ID);
						addedItem.HinhMinhHoa = refName;
						_repCamNang.Save();
					}
				}
			}
			var data = new DataTransferViewModel
			{
                IDLoaiCamNang = int.Parse(idloaicamnang),
				EnumViewModelType = EnumViewModel.ADMIN_CAMNANG,
				Added = result,
			};
			return View("SelectCamNang", CreateViewModel(data));
		}


	}
}